package com.pocket.insight.base.sql.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.pocket.insight.base.sql.constant.ConnectionTypeConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/5
 */
public class SpecifiedVendorContext {
    private static final TransmittableThreadLocal<String> vendorDescInheritableThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> vendorNameTL                     = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<Long>   vendorMajorVersionTL             = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<Long>   vendorMinorVersionTL             = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> vendorProVersionTL               = new TransmittableThreadLocal();


    public static String getVendorDesc() {
        return (String) vendorDescInheritableThreadLocal.get();
    }

    public static String getVendorName() {
        String name = (String) vendorNameTL.get();
        if (name != null) {
            return name;
        }
        return (String) getVendorInfo(Integer.valueOf(0));
    }

    public static Long getVendorMajorVersion() {
        Long majorVersion = (Long) vendorMajorVersionTL.get();
        if (majorVersion != null) {
            return majorVersion;
        }
        return (Long) getVendorInfo(Integer.valueOf(1));
    }

    public static Long getVendorMinorVersion() {
        Long minorVersion = (Long) vendorMinorVersionTL.get();
        if (minorVersion != null) {
            return minorVersion;
        }
        return (Long) getVendorInfo(Integer.valueOf(2));
    }

    public static String getVendorProVersion() {
        String proVersion = (String) vendorProVersionTL.get();
        if (proVersion != null) {
            return proVersion;
        }
        return (String) getVendorInfo(Integer.valueOf(3));
    }

    private static Object getVendorInfo(Integer index) {
        String vendorDesc = (String) vendorDescInheritableThreadLocal.get();
        if (StringUtils.isBlank(vendorDesc)) {
            if (index.equals(Integer.valueOf(1)) || index.equals(Integer.valueOf(2))) {
                return Long.valueOf(0L);
            }
            return null;
        }
        String[] list = vendorDesc.split("_");
        if (list.length > 0) {
            for (Map.Entry<String, String> entry : ConnectionTypeConstant.TYPE_TO_STD_TYPE.entrySet()) {
                String k = entry.getKey();
                if (vendorDesc.startsWith(k)) {
                    if (k.length() == vendorDesc.length()) {
                        list = new String[]{vendorDesc};
                        break;
                    }
                    String moreInfo = vendorDesc.substring(k.length() + 1);
                    list = moreInfo.split("_");
                    String[] temp = new String[list.length + 1];
                    temp[0] = k;
                    System.arraycopy(list, 0, temp, 1, list.length);
                    list = temp;
                    break;
                }
            }
        }
        if (list.length >= 4) {
            String[] temp = new String[4];
            temp[0] = list[0];
            temp[1] = list[1];
            temp[2] = list[2];
            StringBuilder proBuilder = new StringBuilder();
            for (int i = 3; i < list.length; i++) {
                proBuilder.append(list[i])
                        .append('_');
            }
            proBuilder.deleteCharAt(proBuilder.length() - 1);
            temp[3] = proBuilder.toString();
            list = temp;
        }
        switch (index.intValue()) {
            case 0:
                if (list.length > index.intValue()) {
                    String str = list[index.intValue()];
                    vendorNameTL.set(str);
                    return str;
                }
                break;
            case 3:
                if (list.length > index.intValue()) {
                    String str = list[index.intValue()];
                    vendorProVersionTL.set(str);
                    return str;
                }
                break;
            case 1:
                if (list.length > index.intValue()) {
                    try {
                        Long version = Long.valueOf(Long.parseLong(list[index.intValue()]));
                        vendorMajorVersionTL.set(version);
                        return version;
                    } catch (Exception exception) {
                    }
                }

                return Long.valueOf(0L);
            case 2:
                if (list.length > index.intValue()) {
                    try {
                        Long version = Long.valueOf(Long.parseLong(list[index.intValue()]));
                        vendorMinorVersionTL.set(version);
                        return version;
                    } catch (Exception exception) {
                    }
                }
                return Long.valueOf(0L);
        }
        return null;
    }

    public static void setVendorDesc(String vendorDesc) {
        remove();
        vendorDescInheritableThreadLocal.set(vendorDesc);
    }

    public static void remove() {
        vendorDescInheritableThreadLocal.remove();
        vendorNameTL.remove();
        vendorMajorVersionTL.remove();
        vendorMinorVersionTL.remove();
        vendorProVersionTL.remove();
    }
}
